-- 墨过留痕
local moguoliuheng = fk.CreateSkill{
    name = "mcdh_moguoliuheng",
    scope_type = Player.HistoryPhase,
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

moguoliuheng:addEffect(fk.TargetSpecified,{
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if target == player and player:hasSkill(self) and player.phase ==  Player.Play and player:usedSkillTimes(self.name,Player.HistoryPhase) == 0 and data.card.is_damage_card and data.firstTarget then
            local targets = data:getAllTargets()
            table.filter(targets,function(p)return not p:isNude() end)
            for _, id in ipairs(player:getCardIds("he")) do
                if not player:prohibitDiscard(id) then
                    table.insertIfNeed(targets,player.id)
                    break
                end
            end
            if #targets > 0 then
                self.cost_data = targets
                return true
            end
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local to = room:askToChoosePlayers(player,{min_num=1,max_num=1,prompt=  "#moguoliuheng-invoke::",targets = self.cost_data,skill_name=self.name,cancelable = true} )
        if #to >0 then
            self.cost_data = to
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local to = self.cost_data[1]
        local inRange = table.filter(room:getOtherPlayers(player), function(p) return player:inMyAttackRange(p) end)
        if to  == player then
            local id = room:askToDiscard(player,{min_num=1,max_num=1,cancelable=false,include_equip =true,skill_name = self.name,prompt="#moguoliuheng"})
        else
            local id = room:askToChooseCard(player,{target=to,flag="he",skill_name=self.name} )
            room:throwCard(id, self.name, to, player)
        end
        local flag = false
        for _, p in ipairs(room:getOtherPlayers(player)) do
            if (player:inMyAttackRange(p) and not table.contains(inRange, p)) or
                    (not player:inMyAttackRange(p) and table.contains(inRange, p))
            then
                flag =true
                break
            end
        end
        if flag then
            --抄的中流
            local all_skills = Fk.generals[player.general]:getSkillNameList()
            if table.contains(all_skills, self.name) then
                for _, skill_name in ipairs(all_skills) do
                    local skill = Fk.skills[skill_name]
                    local scope_type = skill.scope_type
                    if scope_type == nil and skill.frequency == Skill.Limited then
                        scope_type = Player.HistoryGame
                    end
                    if scope_type and player:usedSkillTimes(skill_name, scope_type) > 0 then
                        player:setSkillUseHistory(skill_name, 0, scope_type)
                    end
                end
            end
            if player.deputyGeneral and player.deputyGeneral ~= "" then
                all_skills = Fk.generals[player.deputyGeneral]:getSkillNameList()
                if table.contains(all_skills, self.name) then
                    for _, skill_name in ipairs(all_skills) do
                        local skill = Fk.skills[skill_name]
                        local scope_type = skill.scope_type
                        if scope_type == nil and skill.frequency == Skill.Limited then
                            scope_type = Player.HistoryGame
                        end
                        if scope_type and player:usedSkillTimes(skill_name, scope_type) > 0 then
                            player:setSkillUseHistory(skill_name, 0, scope_type)
                        end
                    end
                end
            end
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_moguoliuheng"] = "墨过留痕",
    ["#moguoliuheng-invoke"] = "墨过留痕:弃置你或其中一名目标角色的一张牌",
    ["#moguoliuheng"] = "墨过留痕:弃置一张牌",
    [":mcdh_moguoliuheng"] = "出牌阶段限一次，当你使用伤害牌指定目标后，你可以弃置你或其中一名目标角色的一张牌，若有角色因此脱离或进入你的攻击范围，你视为未发动过武将牌上的技能。",
}

return moguoliuheng